package com.taiyi.renaissancemall.ui.login;


import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.AgreementsBean;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.EmptyBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class RegisterPresent extends BasePresent<RegisterView> {


    public void getVerificationCode() {
        Map<String, String> map = new HashMap<>();
        map.put("uniquekey", MyApp.getDeviceUUid());
        map.put("tel", view.getPhoneNumber());
        HttpUtils.postRequest(BaseUrl.GET_VERIFICATION_CODE, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.onSuccess();
                } else {
                    view.onFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }


    public void register() {
        Map<String, String> map = new HashMap<>();
        map.put("uniquekey", MyApp.getDeviceUUid());
        map.put("tel", view.getPhoneNumber());
        map.put("invitecode", view.getInviteCode());
        map.put("smscode", view.getVerificationCode());
        HttpUtils.postRequest(BaseUrl.REGISTER, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.registerSuccess();
                } else {
                    view.onFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }

}
